export const validateName = (rule, value, callback) => {
  if (value === '' || !value) {
    callback(new Error('姓名不能为空'))
    return
  }
  const isChinese = value.search(/^[\u4e00-\u9fa5]+$/g)
  if (isChinese === -1) {
    callback(new Error('姓名必须是中文'))
    return
  }
  callback()
}

export const validateIdCard = (rule, value, callback) => {
  if (value === '' || !value) {
    callback(new Error('身份证号不能为空'))
    return
  }
  const isIdCard = value.search(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/)
  if (isIdCard === -1) {
    callback(new Error('身份证号格式不正确'))
    return
  }
  callback()
}

export const validateIdAddress = (rule, value, callback) => {
  if (value === '' || !value) {
    callback(new Error('籍贯不能为空'))
    return
  }
  const isIdAddress = value.search(/^[\u4e00-\u9fa5]+[省市区]-[\u4e00-\u9fa5]+[市区县]$/)
  if (isIdAddress === -1) {
    callback(new Error('籍贯格式不正确'))
    return
  }
  callback()
}

export const validateAge = (rule, value, callback) => {
  if (value === '' || !value) {
    callback(new Error('年龄不能为空'))
    return
  }
  const isAge = (value + '').search(/^\d{1,4}$/)
  if (isAge === -1 && value >= 0) {
    callback(new Error('年龄格式不正确'))
    return
  }
  callback()
}

export const validateEmpty = (rule, value, callback) => {
  if (value === '' || !value) {
    callback(new Error(`字段不能为空`))
    return
  }
  callback()
}

export const validatePhone = (rule, value, callback) => {
  if (value === '' || !value) {
    callback(new Error('手机号不能为空'))
    return
  }
  const isPhone = value.search(/^\d{11}$/)
  if (isPhone === -1) {
    callback(new Error('手机号格式不正确'))
    return
  }
  callback()
}

export const validateEmail = (rule, value, callback) => {
  if (value === '' || !value) {
    callback(new Error('邮箱不能为空'))
    return
  }
  const isEmail = value.search(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/)
  if (isEmail === -1) {
    callback(new Error('邮箱格式不正确'))
    return
  }
  callback()
}
